package com.ant.collect.service.impl;


import cn.dev33.satoken.secure.BCrypt;
import com.ant.collect.common.ResultCode;
import com.ant.collect.dto.CompanyChangeInfoDto;
import com.ant.collect.handler.AntException;
import com.ant.collect.mapper.CompanyChangeInfoMapper;
import com.ant.collect.mapper.CompanyMapper;
import com.ant.collect.mapper.UserMapper;
import com.ant.collect.pojo.Company;
import com.ant.collect.pojo.CompanyChangeInfo;
import com.ant.collect.pojo.User;
import com.ant.collect.service.ICompanyChangeInfoService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author hzd
 * @since 2025-11-09
 */
@Service
@Slf4j
public class CompanyChangeInfoServiceImpl extends ServiceImpl<CompanyChangeInfoMapper, CompanyChangeInfo> implements ICompanyChangeInfoService {

    private static final Logger logger = LoggerFactory.getLogger(MaterialServiceImpl.class);

    @Resource
    CompanyMapper companyMapper;

    @Resource
    UserMapper userMapper;



    @Override
    @Transactional
    public void review(Integer id) {
        try {
        if (id == null)
            throw new RuntimeException("请输入id");
        CompanyChangeInfo selectOne = baseMapper.selectOne(new QueryWrapper<CompanyChangeInfo>().eq("id", id));
        if (selectOne == null)
            throw new RuntimeException("未找到该信息");

        CompanyChangeInfo info = new CompanyChangeInfo();
        info.setId(id);
        info.setReview(2);
        baseMapper.updateById(info);


        Company company = new Company();
        company.setId(selectOne.getCompanyId());
        company.setCompanyName(selectOne.getCompanyName());
        company.setCompanyCode(selectOne.getCompanyCode());
        company.setBusinessUrl(selectOne.getBusinessUrl());
        company.setPersonName(selectOne.getPersonName());
        company.setContact(selectOne.getContact());
        company.setContactTel(selectOne.getContactTel());
        company.setReview(1);
        companyMapper.updateById(company);

        Company selectOne1 = companyMapper.selectOne(new QueryWrapper<Company>().eq("id", selectOne.getCompanyId()));
        User user = new User();
        user.setId(selectOne1.getUserId());
        user.setUserName(selectOne.getContactTel());
        user.setUserPwd(selectOne.getPassword());
        userMapper.updateById(user);
        }catch (Exception e){
            logger.info("操作失败"+e.getMessage());
            throw new RuntimeException(e.getMessage());
        }

    }

    @Override
    public void save(CompanyChangeInfoDto dto) {
        try {
            if (ObjectUtils.isEmpty(dto.getContact())) {
                throw new RuntimeException("请输入联系人");
            }
            if (ObjectUtils.isEmpty(dto.getContactTel())) {
                throw new RuntimeException("请输入联系人电话");
            }
            if (ObjectUtils.isEmpty(dto.getCompanyCode())) {
                throw new RuntimeException("请输入组织机构代码");
            }
            if (ObjectUtils.isEmpty(dto.getPassword())) {
                throw new RuntimeException("请输入新密码");
            }

            Company company = companyMapper.selectOne(new QueryWrapper<Company>().eq("company_code", dto.getCompanyCode()));
            if (ObjectUtils.isEmpty(company)) {
                throw new RuntimeException("组织机构代码不存在");
            }

            String encryptedPassword = BCrypt.hashpw(dto.getPassword(), BCrypt.gensalt(12));
            CompanyChangeInfo info = new CompanyChangeInfo();
            info.setCompanyName(company.getCompanyName());
            info.setCompanyCode(dto.getCompanyCode());
            info.setBusinessUrl(dto.getBusinessUrl());
            info.setPersonName(dto.getPersonName());
            info.setContact(dto.getContact());
            info.setContactTel(dto.getContactTel());
            info.setReview(1);
            info.setCompanyId(company.getId());
            info.setPassword(encryptedPassword);
            info.setCreationTime(new Date());
            baseMapper.insert(info);
        } catch (Exception e) {
            logger.info("保存失败");
            throw new RuntimeException(e.getMessage());
        }
    }

    @Override
    public Map<String, Object> queryCompanyList(CompanyChangeInfoDto dto) {
        try {
            Page<CompanyChangeInfo> pageInfo = new Page<>(dto.getPageIndex(), dto.getPageSize());
            QueryWrapper<CompanyChangeInfo> wrapper = new QueryWrapper<>();
            if (!ObjectUtils.isEmpty(dto.getCompanyName())) {
                wrapper.eq("company_name", dto.getCompanyName().trim());
            }
            wrapper.orderByDesc("id");
            Page<CompanyChangeInfo> list = baseMapper.selectPage(pageInfo, wrapper);
            List<CompanyChangeInfo> companyList = list.getRecords();
            Map<String, Object> map = new HashMap<>();
            map.put("list", companyList);
            map.put("total", list.getTotal());
            return map;
        } catch (Exception e) {
            throw new AntException(ResultCode.BODY_NOT_MATCH);
        }
    }
}
